<?php 

namespace Weixin\Controller;
use Common\Controller\WeixinBase;
use Common\Model\Model;

class UcenterController extends WeixinbaseController 
{
    /**
     * 个人主页
     */
    public function index()
    {
        $userid = I('get.id');
        if(empty($userid))
        {
            header('Location:'.WEB_URL.U('User/index'));
            exit;
        }
        if($this->userid && $this->userid == $userid)
        {
            header('Location:'.WEB_URL.U('User/index'));
            exit;
        }
        $user = D('Weixin/User');
        $userInfo = $user->getUserInfo($userid);
        if(empty($userInfo))
        {
            header('Location:'.WEB_URL.U('User/index'));
            exit;
        }
        $workNum = $user->getWorkNum($userid, 1);
        $suipaiNum = $user->getSuipaiNum($userid, 1);
        $teachNum = $user->getTeachNum($userid);
        
        $type = empty($_GET['type']) ? 4 : $_GET['type'];
        $this->assign('type', $type);
        
        switch($type)
        {
            case 1:
                $this->getWork();
                break;
            case 4:
                $this->getSuipai();
                break;
            case 7:
                $this->getTeach();
                break;
            default:
                $this->getSuipai();
        }
        $this->assign('userid', $userid);
        $this->assign('userInfo', $userInfo);
        $this->assign('teachNum', $teachNum);
        $this->assign('workNum', $workNum);
        $this->assign('suipaiNum', $suipaiNum);
        $this->assign('pageType', 'user');
        $this->assign('myid', $this->userid);
        $this->getJsdk();
        $this->display();
    }
    
    /**
     * 获取随手拍列表
     */
    public function getSuipai()
    {
        $userid = I('get.id');
        $user = D('Weixin/User');
        $count = $user->getSuipaiNum($userid, 1);
        $page = $this->Pages($count, 4);
    
        $list = $user->getSpList($userid, $page->firstRow,$page->listRows, 1);
        if($_GET['code'] == 'getPage')
        {
            if(count($list) > 0)
            {
                $this->success($list);
            }
            $this->error('fail');
        }
        $this->assign('list', $list);
    }
    
    /**
     * 获取食谱列表
     */
    public function getWork()
    {
        $userid = I('get.id');
        $user = D('Weixin/User');
        $count = $user->getWorkNum($userid, 1);
        $page = $this->Pages($count, 5);
    
        $list = $user->getWorkList($userid, $page->firstRow, $page->listRows, 1);
        if($_GET['code'] == 'getPage')
        {
            if(count($list) > 0)
            {
                $this->success($list);
            }
            $this->error('fail');
        }
        $this->assign('list', $list);
    }
    
    /**
     * 获取导师列表
     */
    public function getTeach()
    {
        $userid = I('get.id');
        $user = D('Weixin/User');
        $count = $user->getTeachNum($userid);
        $page = $this->Pages($count, 5);
    
        $list = $user->getTeachList($userid, $page->firstRow, $page->listRows);
        if($_GET['code'] == 'getPage')
        {
            if(count($list) > 0)
            {
                $this->success($list);
            }
            $this->error('fail');
        }
        $this->assign('list', $list);
    }
    
    public function fans()
    {
        $userid = I('get.id');
        if(!intval($userid))
        {
            header('Location:'.WEB_URL.U('User/index'));
            exit;
        }
        $model = M('UserFans');
        $count = M('UserFans')->where("is_del=0 and userid=%d and status=1", array($userid))->count();
         
        $page = $this->Pages($count, 10);
        $field = array('id', 'myid', 'create_time', 'userid');
        $list = $model->field($field)->where("is_del=0 and userid=%d and status=1", array($userid))->order('create_time desc')->limit($page->firstRow . ',' . $page->listRows)->select();
        if($list)
        {
            foreach($list as $k => $v)
            {
                $user = M('User')->where(array("userid" => $v['myid']))->find();
                $list[$k]['nickname'] = $user['nickname'];
                $list[$k]['openuid'] = $user['openuid'];
                $list[$k]['uavatar'] = $user['uavatar'];
                $list[$k]['isFans'] = M('UserFans')->where(array('myid' => $v['userid'], 'userid' => $v['myid'], 'status' => true))->count();
            }
        }
        if($_GET['code'] == 'getPage')
        {
            if(count($list) > 0)
            {
                $this->success($list);
            }
            $this->error('fail');
        }
        $this->assign("list", $list);
        $this->assign("userid", $userid);
        $this->display();
    }
    
}